package com.btpn.custody.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.btpn.custody.dao.IArea;
import com.btpn.custody.dao.IRegion;
import com.btpn.custody.entity.Area;
import com.btpn.custody.entity.Region;
import com.btpn.custody.service.IAreaService;

@Service("areaService")
@Transactional
public class AreaServiceImpl implements IAreaService {
	
	@Autowired @Qualifier("areaDaoHibernate")
	private IArea areaDao;

	@Autowired
	private IRegion regionDaoHibernate;
	
	@Override
	public void createArea(Area area, Long regionId) {
		// get region by regionId
		Region region = regionDaoHibernate.get(regionId);
		
		// set region to area
		area.setRegion(region);
		
		areaDao.create(area);
	}

//	@Override
//	public List<Area> getAllArea() {
//		return areaDao.getAll();
//	}

//	@Override
//	public Area getArea(Long id) {
//		return areaDao.get(id);
//	}

	@Override
	public void updateArea(Area area, Long regionId) {
		// get region by regionId
		Region region = regionDaoHibernate.get(regionId);
		
		// set region to area
		area.setRegion(region);
		
		areaDao.update(area);
	}

//	@Override
//	public void deleteArea(Long id) {
//		areaDao.delete(id);
//	}
//
//	@Override
//	public Integer getAreaCount() {
//		return areaDao.getCount();
//	}
//
//	@Override
//	public void addOrUpdateArea(Area area) {
//		areaDao.createOrUpdate(area);
//	}

	@Override
	public void deleteAreas(Long regionId) {
		List<Area> areas = regionDaoHibernate.get(regionId).getAreas();
		
		for (Area area : areas) {
			areaDao.delete(area.getId());
		}
	}
}
